home *** CD-ROM | disk | FTP | other *** search
- /****************************************************************************************
- Aligned.h
- <http://redshed.net/align>
-
- The buzzard told the monkey "You are chokin' me
- "Release your hold and I will set you free".
- The monkey looked the buzzard right dead in the eye
- And said, "Your story's so touching, it sounds just like a lie".
-
- Copyright © 1999-2001 Red Shed Software. All rights reserved.
- by Jonathan 'Wolf' Rentzsch (jon@redshed.net)
-
- Redistribution and use in source and binary forms, with or without modification,
- are permitted provided that the following conditions are met:
-
- 1. Redistributions of source code must retain the above copyright notice, this
- list of conditions and the following disclaimer.
-
- 2. Redistributions in binary form must reproduce the above copyright notice, this
- list of conditions and the following disclaimer in the documentation and/or other
- materials provided with the distribution.
-
- 3. The names and trademarks of copyright holders may not be used in advertising
- or publicity pertaining to the software without specific prior permission.
-
- This software is provided "as is" and any expressed or implied warranties,
- including, but not limited to, the implied warranties of merchantability and
- fitness for a particular purpose are disclaimed. In no event shall the authors
- or copyright holders be liable for any direct, indirect, incidental, special,
- exemplary, or consequential damages (including, but not limited to, procurement
- of substitute goods or services; loss of use, data, or profits; or business
- interruption) however caused and on any theory of liability, whether in contract,
- strict liability, or tort (including negligence or otherwise) arising in any way
- out of the use of this software, even if advised of the possibility of such
- damage.
-
- Commenter Date Comment
- --------- ----------------- -----------------------------------------------------
- wolf Fri, Feb 12, 1999 Created (broken out from Align.h).
- wolf Fri, Aug 6, 1999 Removed CWPro2 placement new work-around.
- Un-inlined Align::Initialize().
- wolf Tue, Aug 10, 1999 Removed Align::Initialize() -- it was only used for
- debugging purposes.
- wolf Mon, Dec 11, 2000 Align 1.0.
- wolf Thu, Feb 8, 2001 Align 1.0.2. CodeWarrior Pro 6 breaks an idiom:
- implicit casting of <type>** to void**. I had to make
- all such casts explicit.
-
- ************************************************************************************/
-
- #ifndef _Aligned_
- #define _Aligned_
-
- #include "Align.h"
- #include <new>
-
- /* wolf -- Fri, Aug 6, 1999: CWPro5 fixes the behavior documented below.
- //#include <new.h>
- // Sadly, CWPro2's placement new operator is defined as throw(),
- // which causes an illegal function overloading error which I can't figure out
- // how to fix. So instead of #including new.h, I just define my own placement new
- // operator here.
- inline
- void*
- operator new(
- size_t,
- void *location )
- { return( location ); }*/
-
- template <class TYPE, UInt8 ALIGNMENT = 4>
- class Aligned {
- public:
- Aligned()
- : ptr_( (TYPE*) this )
- { AlignX( &ptr_, ALIGNMENT );
- ptr_ = new (ptr_) TYPE; }
-
- Aligned(
- const TYPE &value )
- : ptr_( (TYPE*) this )
- { AlignX( (void**) &ptr_, ALIGNMENT );
- ptr_ = (TYPE*) (new (ptr_) TYPE( value )); }
-
- ~Aligned()
- { ptr_->~TYPE(); }
-
- operator TYPE() const
- { return( *(TYPE*) ptr_ ); }
-
- TYPE& operator*()
- { return( *ptr_ ); }
-
- TYPE* operator->()
- { return( ptr_ ); }
-
- TYPE* operator&()
- { return( ptr_ ); }
- private:
- UInt8 padding_[ ALIGNMENT ];
- UInt8 storage_[ sizeof( TYPE ) ];
- TYPE *ptr_;
- };
-
- template <class TYPE, UInt8 ALIGNMENT = 4>
- class OldAligned {
- public:
- OldAligned()
- : data_(),
- ptr_( this )
- { for( short i; i < ALIGNMENT; ++i )
- padding_[ i ] = 0;
- AlignX( &ptr_, ALIGNMENT ); }
-
- OldAligned(
- const TYPE &value )
- : data_( value ),
- ptr_( this )
- { for( short i; i < ALIGNMENT; ++i )
- padding_[ i ] = 0;
- AlignX( &ptr_, ALIGNMENT ); }
-
- operator TYPE() const
- { return( *(TYPE*) ptr_ ); }
-
- TYPE& operator*()
- { return( *(TYPE*) ptr_ ); }
-
- TYPE* operator->()
- { return( (TYPE*) ptr_ ); }
-
- TYPE* operator&()
- { return( (TYPE*) ptr_ ); }
- private:
- UInt8 padding_[ ALIGNMENT ];
- TYPE data_;
- void *ptr_;
- };
-
- #endif // _Aligned_